package com.lollipop.ucenter.web;

import com.github.pagehelper.PageInfo;
import com.lollipop.ucenter.rpc.api.RoleService;
import com.lollipop.ucenter.rpc.model.Role;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashSet;
import java.util.Set;

@RestController
@RequestMapping("/role")
@Slf4j
public class RoleController {

    @Autowired
    RoleService roleService;

    @RequestMapping("/user/{id}")
    public Set<String> findRolesByUserId(@PathVariable("id") String userId) {
        Set<Role> roles = roleService.findRolesByUserId(userId);
        Set<String> ret = new HashSet<String>();
        if (CollectionUtils.isNotEmpty(roles)) {
            for (Role role : roles) {
                ret.add(role.getId());
            }
        }
        return ret;
    }

    @RequestMapping("/page")
    public PageInfo<Role> findPager(
            @RequestParam("page") int pageNo, @RequestParam("limit") int pageSize) {
        if (log.isDebugEnabled())
            log.debug(">>> params pageNo is {}, pageSize is {}", pageNo, pageSize);
        return roleService.findPager(pageNo, pageSize, new Role());
    }
}
